/*
 * Copyright (C) 2009 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.accessor;

import java.util.EnumMap;
import java.util.List;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;

import cc.aileron.commons.util.SkipList;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * @author Aileron
 */
@Singleton
public class PojoAccessorMixerImpl implements PojoAccessorMixer
{
    @Override
    public PojoAccessor<?> mixin(final List<PojoAccessor<?>> accessors)
    {
        final EnumMap<PojoAccessorMethod, List<String>> keys = new EnumMap<PojoAccessorMethod, List<String>>(
                PojoAccessorMethod.class);
        keys.put(PojoAccessorMethod.GET, new SkipList<String>());
        keys.put(PojoAccessorMethod.SET, new SkipList<String>());
        for (final PojoAccessor<?> accessor : accessors)
        {
            for (final PojoAccessorMethod method : PojoAccessorMethod.values())
            {
                final List<String> methodKeys = accessor.keys(method);
                keys.get(method)
                    .addAll(methodKeys);
            }
        }
        final PojoAccessorManager manager = this.manager;
        return new PojoAccessor<Object>()
        {
            @Override
            public boolean exist(final String key)
            {
                for (final PojoAccessor<?> accessor : accessors)
                {
                    if (accessor.exist(key))
                    {
                        return true;
                    }
                }
                return false;
            }

            @Override
            public List<String> keys(final PojoAccessorMethod method)
            {
                return keys.get(method);
            }

            @Override
            public PojoAccessor<?> mixin(final Object... objects)
            {
                final SkipList<PojoAccessor<?>> list = new SkipList<PojoAccessor<?>>();
                list.add(this);
                for (final Object object : objects)
                {
                    list.add(manager.from(object));
                }
                return mixin(list);
            }

            @Override
            public PojoAccessorValue to(final String key) throws PojoAccessorValueNotFoundException,
                    PojoPropertiesNotFoundException
            {
                for (final PojoAccessor<?> accessor : accessors)
                {
                    if (accessor.exist(key))
                    {
                        return accessor.to(key);
                    }
                }
                throw new PojoAccessorValueNotFoundException(
                        PojoAccessorMixer.class, key);
            }

            @Override
            public Object toTarget()
            {
                throw new UnsupportedOperationException();
            }

            @Override
            public String toString()
            {
                return ReflectionToStringBuilder.toString(this);
            }
        };
    }

    /**
     * @param manager
     */
    @Inject
    public PojoAccessorMixerImpl(final PojoAccessorManager manager)
    {
        this.manager = manager;
    }

    private final PojoAccessorManager manager;
}
